﻿using System;
using System.ServiceModel;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;

namespace droid
{
    [Activity(Label = "Välj karaktär")]
    public class SelectActivity : ListActivity
    {
        string[] items;

        //se.raspare.wcf.WcfRaspare wcfService;

        string characterName = string.Empty;

        public static readonly EndpointAddress EndPoint = new EndpointAddress("http://wcf.raspare.se/WcfRaspare.svc");

        private WcfRaspareClient _client;
        private ProgressDialog progressDialog;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //Check if we already have a chosen character
            if (bundle != null)
            {
                characterName = bundle.GetString("CharacterName", string.Empty);
            }

            ////Debug
            //characterName = "John Richter";

            if (!String.IsNullOrWhiteSpace(characterName))
            {
                var showActivity = new Intent(this, typeof(OldShowActivity));
                showActivity.PutExtra("CharacterName", characterName);
                StartActivity(showActivity);
                return;
            }

            InitializeWcfServiceClient();

            //Load screen
            //wcfService = new se.raspare.wcf.WcfRaspare();
            string name = Intent.GetStringExtra("Username") ?? string.Empty;
            //items = wcfService.GetMyDekaederCharacterList(name);

            if (!String.IsNullOrWhiteSpace(name))
            {
                progressDialog = ProgressDialog.Show(this, "Var god vänta...", "Hämtar data från servern...", true);
                _client.GetMyDekaederCharacterListAsync(name);
            }



        }

        private void InitializeWcfServiceClient()
        {
            BasicHttpBinding binding = CreateBasicHttp();

            _client = new WcfRaspareClient(binding, EndPoint);
            _client.GetMyDekaederCharacterListCompleted += ClientOnCompleted;
        }

        private static BasicHttpBinding CreateBasicHttp()
        {
            BasicHttpBinding binding = new BasicHttpBinding
            {
                Name = "basicHttpBinding",
                MaxBufferSize = 2147483647,
                MaxReceivedMessageSize = 2147483647
            };
            TimeSpan timeout = new TimeSpan(0, 2, 30);
            binding.SendTimeout = timeout;
            binding.OpenTimeout = timeout;
            binding.ReceiveTimeout = timeout;
            return binding;
        }

        private void ClientOnCompleted(object sender, GetMyDekaederCharacterListCompletedEventArgs e)
        {
            RunOnUiThread(() => progressDialog.Hide());
            
            if (!e.Cancelled)
            {
                items = e.Result;
                RunOnUiThread(() => ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items));
            }
        }

        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            var character = items[position];
            var showActivity = new Intent(this, typeof(OldShowActivity));
            showActivity.PutExtra("CharacterName", character);
            StartActivity(showActivity);
        }
        
        protected override void OnSaveInstanceState(Bundle outState)
        {
            outState.PutString("CharacterName", characterName);

            // always call the base implementation!
            base.OnSaveInstanceState(outState);
        }
    }
}

